//
//  ShowTextCell.swift
//  Swift-Animations
//
//  Created by YouXianMing on 16/8/30.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

import UIKit

enum EShowTextCellType : Int {
    
    case normalType, expendType
}

private let heitiSC : UIFont = UIFont.HeitiSC(16)
private let numberOfLines : Int = 5

class ShowTextCell: CustomCell {
    
    /// 点击选中时显示的文字
    fileprivate var expendLabel : UILabel!
    /// 未选中的文字
    fileprivate var normalLabel : UILabel!
    fileprivate var topLineView : UIView!
    /// 底部view
    fileprivate var lineView : UIView!
    /// 左侧红色view
    fileprivate var redView : UIView!
    fileprivate var imageV : UIImageView!
    override func buildSubview() {
        imageV = UIImageView()
        imageV.backgroundColor = UIColor.red
       
        addSubview(imageV)
        imageV.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(self)?.offset()(10)
            make?.left.mas_equalTo()(self)?.offset()(10)
            make?.height.mas_equalTo()(50)
            make?.width.mas_equalTo()(50)
        }
        
        topLineView = UIView()
        topLineView.backgroundColor = UIColor.black.alpha(0.5)
        addSubview(topLineView)
        topLineView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(self)
            make?.left.mas_equalTo()(self)
            make?.right.mas_equalTo()(self)
            make?.height.mas_equalTo()(0.5)
        }
        
        lineView = UIView()
        lineView.backgroundColor = UIColor.black.alpha(0.5)
        addSubview(lineView)
        lineView.mas_makeConstraints { (make) in
            make?.bottom.mas_equalTo()(self)
            make?.left.mas_equalTo()(self)
            make?.right.mas_equalTo()(self)
            make?.height.mas_equalTo()(0.5)
        }
        
        normalLabel = UILabel()
        normalLabel.font = heitiSC
        addSubview(normalLabel)
        normalLabel.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(topLineView.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(imageV.mas_right)?.offset()(5)
            make?.right.mas_equalTo()(self)?.offset()(-10)
            make?.bottom.mas_equalTo()(lineView.mas_top)?.offset()(-5)
        }
        
        //正文
        expendLabel = UILabel()
        expendLabel.font = heitiSC
        expendLabel.textColor = UIColor.black.alpha(1)
        addSubview(expendLabel)
        expendLabel.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(topLineView.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(imageV.mas_right)?.offset()(5)
            make?.right.mas_equalTo()(self)?.offset()(-10)
            make?.bottom.mas_equalTo()(lineView.mas_top)?.offset()(-5)
        }
        
        //左侧红色view
        redView = UIView(frame: CGRect(x: 0, y: 20, width: 5, height: 30))
        redView.backgroundColor = UIColor.red
        addSubview(redView)
    }
    
    override func loadContent() {
        
        (indexPath as NSIndexPath?)?.row == 0 ? (lineView.isHidden = true) : (lineView.isHidden = false)
        changeStateWithCellType((dataAdapter?.cellType!)!)
    }
    
    fileprivate func changeStateWithCellType(_ type : Int) {
        
        let model = data as! ShowTextModel
        
//        normalLabel.sizeToFitWithString(model.inputString!, width: Screen.Width - 30, numberOfLines: numberOfLines)
//        normalLabel.left = 15
//        normalLabel.top = 15
        normalLabel.numberOfLines = numberOfLines
        normalLabel.text = model.inputString
        
//        expendLabel.sizeToFitWithString(model.inputString!, width: Screen.Width - 30, numberOfLines: 0)
//        expendLabel.left = 15
//        expendLabel.top = 15
        expendLabel.numberOfLines = 0
        expendLabel.text = model.inputString
        
        if type == EShowTextCellType.normalType.rawValue {
            //未点击时的显示
            expendLabel.alpha = 0
            normalLabel.alpha = 1
            redView.backgroundColor = UIColor.gray
            
        } else if type == EShowTextCellType.expendType.rawValue {
            ///点击时显示的文字
            expendLabel.alpha = 1
            normalLabel.alpha = 0
            redView.backgroundColor = UIColor.red
        }
    }
    
    override func selectedEvent() {
        
        let model = data as! ShowTextModel
        
        if dataAdapter?.cellType == EShowTextCellType.normalType.rawValue {
            
            self.dataAdapter?.cellType = EShowTextCellType.expendType.rawValue
            self.updateWithNewCellHeight(model.expendStringHeight!)
            
            UIView.animate(withDuration: 0.25, animations: {
                
                self.normalLabel.alpha = 0
                self.expendLabel.alpha = 1
                self.redView.backgroundColor = UIColor.red
            })
            
        } else if dataAdapter?.cellType == EShowTextCellType.expendType.rawValue {
            
            self.dataAdapter?.cellType = EShowTextCellType.normalType.rawValue
            self.updateWithNewCellHeight(model.normalStringHeight!)
            
            UIView.animate(withDuration: 0.25, animations: {
                
                self.normalLabel.alpha = 1
                self.expendLabel.alpha = 0
                self.redView.backgroundColor = UIColor.gray.alpha(0.5)
            })
        }
    }
    
    override class func HeightWithData(_ data : AnyObject?) -> CGFloat {
        
        let model = data as! ShowTextModel
        model.expendStringHeight = 60 + (model.inputString?.heightWithFont(heitiSC, fixedWidth: Screen.Width - 60))!
        
        model.normalStringHeight = 15 + String.HeightWithFont(heitiSC) * CGFloat(numberOfLines)
        return model.normalStringHeight!
    }
}

